/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.NguoiDungPOJO;
import java.sql.ResultSet;
import java.util.ArrayList;

/** @author
 *  ITEC.HCMUS.EDU.VN
 *  Ngô Huy Đạt
 *  086%s005
 *  nhdat.itec@gmail.com
 */
public class NguoiDungDAO {

    public static ArrayList<NguoiDungPOJO> selectAll(){
        ArrayList<NguoiDungPOJO> list = new ArrayList<NguoiDungPOJO>();
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = "SELECT * FROM nguoidung";
            ResultSet rs = helper.excecuteQuery(sql);
            while(rs.next()){
                NguoiDungPOJO n = new NguoiDungPOJO();
                n.setTenNguoiDung(rs.getString("TenNguoiDung"));
                n.setTaiKhoan(rs.getString("TaiKhoan"));
                n.setMatMa(rs.getString("MatMa"));
                n.setQLNamHoc(rs.getBoolean("QLNamHoc"));
                n.setQLLop(rs.getBoolean("QLLop"));
                n.setQLHocSinh(rs.getBoolean("QLHocSinh"));
                n.setQLMonHoc(rs.getBoolean("QLMonHoc"));
                n.setQLQuyDinh(rs.getBoolean("QLQuyDinh"));
                n.setDSHocSinh(rs.getBoolean("DSHocSinh"));
//                n.setDSLop(rs.getBoolean("DSLop"));
                n.setXepLop(rs.getBoolean("XepLop"));
                n.setNhapDiem(rs.getBoolean("NhapDiem"));
                n.setBCMon(rs.getBoolean("BCMon"));
                list.add(n);
            }

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return list;
    }

    public static boolean insert(String TaiKhoan,String TenNguoiDung, String MatMa,String QLNamHoc, String QLLop, String QLHocSinh, String QLMon, String QLQuyDinh,String DSHocSinh, String XepLop,String NhapDiem, String BCMon){
        boolean kq = false;
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("INSERT INTO nguoidung (TaiKhoan, TenNguoiDung, MatMa, QLNamHoc, QLLop, QLHocSinh, QLMonHoc, QLQuyDinh, DSHocSinh, DSLop, XepLop, NhapDiem, BCMon) "
                    + "VALUES ('%s', '%s', '%s', %s, %s, %s, %s, %s, %s, false, %s, %s, %s)",TaiKhoan, TenNguoiDung, MatMa, QLNamHoc, QLLop, QLHocSinh, QLMon, QLQuyDinh, DSHocSinh, XepLop,NhapDiem,BCMon);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return kq;
    }


    public static boolean  delete(String TaiKhoan) {
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("DELETE FROM nguoidung WHERE TaiKhoan = '%s'",TaiKhoan);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }


    public static boolean  update(String TaiKhoan,String TenNguoiDung, String MatMa,String QLNamHoc, String QLLop, String QLHocSinh, String QLMon, String QLQuyDinh,String DSHocSinh, String XepLop,String NhapDiem, String BCMon){
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("UPDATE nguoidung SET  TenNguoiDung = '%s', MatMa = '%s', "
                    + "QLNamHoc = %s, QLLop = %s, QLHocSinh = %s, QLMonHoc = %s, QLQuyDinh = %s, DSHocSinh = %s, DSLop = false, XepLop = %s, NhapDiem = %s, BCMon = %s "
                    + "WHERE TaiKhoan = '%s'",TenNguoiDung, MatMa, QLNamHoc, QLLop, QLHocSinh, QLMon, QLQuyDinh, DSHocSinh, XepLop,NhapDiem,BCMon,TaiKhoan);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }

}
